package com.googlecode.aleverr.yafotkiclient.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import com.googlecode.aleverr.yafotkiclient.R;
import com.googlecode.aleverr.yafotkiclient.api.FormDataGenerator;
import com.googlecode.aleverr.yafotkiclient.api.GlobalSettings;
import com.googlecode.aleverr.yafotkiclient.api.Photo;

import java.io.File;

public class UploadPhotoActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "UploadPhotoActivity";

    private static final int UPLOAD_FORM_INDEX = 0;
    private static final int PROGRESS_BAR_INDEX = 1;

    private String filePath;
    private String albumId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload_photo);

        findViewById(R.id.upload).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
        ((ViewSwitcher) findViewById(R.id.viewSwitcher)).setDisplayedChild(UPLOAD_FORM_INDEX);

        Intent intent = getIntent();
        filePath = intent.getStringExtra("filePath");
        albumId = intent.getStringExtra("albumId");

        new LoadThumbnailTask().execute(filePath);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.upload:
                FormDataGenerator generator = new FormDataGenerator();
                EditText photoName = (EditText) findViewById(R.id.photoName);
                generator.addTextSection("title", photoName.getText().toString());
                if (albumId != null && !"".equals(albumId)) {
                    generator.addTextSection("album", albumId);
                }
                generator.addJpegSection("image", filePath);
                new UploadPhotoTask().execute(generator);
                break;
            case R.id.cancel:
                setResult(RESULT_CANCELED);
                finish();
                break;
        }
    }

    private class UploadPhotoTask extends AsyncTask<FormDataGenerator, Void, Photo> {

        @Override
        protected Photo doInBackground(FormDataGenerator... generators) {
            if (generators.length > 0) {
                GlobalSettings settings = GlobalSettings.getInstance(UploadPhotoActivity.this);
                String uploadAddress = settings.getUserHome().getPhotoCollectionAddress();
                String accessToken = settings.getAccessToken();
                Photo photo = Photo.uploadPhoto(uploadAddress, accessToken, generators[0]);
                new File(filePath).delete();
                return photo;
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            ((ViewSwitcher) findViewById(R.id.viewSwitcher)).setDisplayedChild(PROGRESS_BAR_INDEX);
        }

        @Override
        protected void onPostExecute(Photo photo) {
            Intent result = new Intent();
            if (photo != null) {
                result.putExtra("uploaded", true);
            } else {
                result.putExtra("uploaded", false);
            }
            setResult(RESULT_OK, result);
            finish();
        }
    }
    
    private class LoadThumbnailTask extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... strings) {
            if (strings.length == 0) {
                return null;
            }
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(strings[0], options);
            int scale = (int) Math.ceil(options.outWidth / 200.0);
            options = new BitmapFactory.Options();
            options.inSampleSize = scale;
            return BitmapFactory.decodeFile(strings[0], options);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                ((ImageView) findViewById(R.id.thumbnailImage)).setImageBitmap(bitmap);
            }
        }
    }
}